//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.games.CraftingEngine;

import com.comprosoft.telnet.command.Command;
import com.comprosoft.telnet.command.CommandResult;
import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.io.TextColor;
import com.comprosoft.telnet.server.TelnetServer;
import java.io.IOException;
import java.util.regex.Pattern;

public class CraftingEngine extends Command {
    private ElementCollection game;
    private boolean running;

    public CraftingEngine(IOHandler io, TelnetServer telnetServer) throws IOException {
        super(io, "craft", telnetServer);
        this.game = new ElementCollection(io);
    }

    public void RunGame() {
        IOHandler io = super.getIOHandler();
        io.ClearScreen();
        DrawTitle(io);
        this.running = true;

        while(this.running) {
            io.SetTextColor(TextColor.WHITE);
            io.Write("> ");
            io.Flush();

            try {
                String command = io.readLine();
                command = command.trim();
                command = command.replaceAll("\\P{Print}", "");
                if (!this.runCommand(command)) {
                    this.game.Craft(command);
                }
            } catch (IOException var3) {
                var3.printStackTrace();
            }
        }

    }

    private boolean runCommand(String command) {
        IOHandler io = super.getIOHandler();
        command = command.toLowerCase();
        if (command.split(Pattern.quote(" "))[0].equals("list")) {
            this.game.List(command);
            return true;
        } else if (command.equals("exit")) {
            this.running = false;
            return true;
        } else if (command.equalsIgnoreCase("help")) {
            DrawHelp(io);
            return true;
        } else if (command.equals("clear")) {
            io.ClearScreen();
            return true;
        } else if (command.equals("reset")) {
            this.game.Reset();
            return true;
        } else if (command.equals("hint")) {
            this.game.Hint();
            return true;
        } else {
            return false;
        }
    }

    public static void DrawTitle(IOHandler io) {
        io.SetTextColor(TextColor.GREEN);
        io.WriteLine(" _____            __ _   _                _____            _            ");
        io.WriteLine("/  __ \\          / _| | (_)              |  ___|          (_)           ");
        io.WriteLine("| /  \\/_ __ __ _| |_| |_ _ _ __   __ _   | |__ _ __   __ _ _ _ __   ___ ");
        io.WriteLine("| |   | '__/ _` |  _| __| | '_ \\ / _` |  |  __| '_ \\ / _` | | '_ \\ / _ \\");
        io.WriteLine("| \\__/\\ | | (_| | | | |_| | | | | (_| |  | |__| | | | (_| | | | | |  __/");
        io.WriteLine(" \\____/_|  \\__,_|_|  \\__|_|_| |_|\\__, |  \\____/_| |_|\\__, |_|_| |_|\\___|");
        io.WriteLine("                                  __/ |               __/ |             ");
        io.WriteLine("(C) Comprosoft 2016              |___/               |___/              ");
        io.WriteLine("  *Based on Little Alchemy by Jakub Koziol");
        io.WriteLine("\n\n");
        io.SetTextColor(TextColor.CYAN);
        io.WriteLine("Type in a recepie with:");
        io.SetTextColor(TextColor.MAGENTA);
        io.Write("   (Element)");
        io.SetTextColor(TextColor.WHITE);
        io.Write(" + ");
        io.SetTextColor(TextColor.YELLOW);
        io.WriteLine("(Element)\n");
        io.SetTextColor(TextColor.GRAY);
        io.WriteLine("  Type 'list' to show available elements");
        io.SetTextColor(TextColor.RED);
        io.WriteLine("  Type 'help' to show additional help");
        io.SetTextColor(TextColor.WHITE);
        io.WriteLine("\n");
    }

    public static void DrawHelp(IOHandler io) {
        io.ClearScreen();
        io.SetTextColor(TextColor.GREEN);
        io.WriteLine("Help Screen:");
        io.WriteLine("------------");
        io.SetTextColor(TextColor.CYAN);
        io.WriteLine("Type in a recepie with:");
        io.SetTextColor(TextColor.MAGENTA);
        io.Write("   (Element)");
        io.SetTextColor(TextColor.WHITE);
        io.Write(" + ");
        io.SetTextColor(TextColor.YELLOW);
        io.WriteLine("(Element)\n");
        io.SetTextColor(TextColor.GREEN);
        io.WriteLine("\nAll Commands:");
        io.WriteLine("--------------");
        io.SetTextColor(TextColor.GRAY);
        io.WriteLine("  (Element) = show the crafting recepie(s) for that element");
        io.SetTextColor(TextColor.RED);
        io.WriteLine("  list [L] = show available elements");
        io.WriteLine("     [L] (optional) = Starting letter(s), number(s), or symbol(s)");
        io.WriteLine("");
        io.SetTextColor(TextColor.YELLOW);
        io.WriteLine("  hint = Show a random crafting recepie using unlocked elements");
        io.WriteLine("");
        io.SetTextColor(TextColor.CYAN);
        io.WriteLine("  clear = clear the screen");
        io.SetTextColor(TextColor.GREEN);
        io.WriteLine("  help = display all commands");
        io.SetTextColor(TextColor.RED);
        io.WriteLine("  exit = quit the crafting engine game");
        io.SetTextColor(TextColor.WHITE);
        io.WriteLine("");
    }

    public CommandResult handleCommand(String[] args) {
        this.RunGame();
        return CommandResult.COMMAND_RESPLASH;
    }
}
